const mysql = require("mysql");

var statusobj = require('./routerStatus')

/**
 * @param {
*   database:'second_server',
*   table_use:'list',
*   listTableProperty:[
*       id(auto up,not null),name(not null),birth(not null),updateDate(系统生成)
*   ],
*   warn:'每个接口在最后一次操作完表之后要释放内存'
* }
*/

const pool = mysql.createPool({
    host: "localhost", port: 3306, user: "root", password: "root", database: "second_server", connectionLimit: 20, multipleStatements: true
});// 将数据库连接池对象导出

const forsqlinit = function () {
    return new Promise((res, rej) => {
        var flag = ''
        var sta = 0
        flag = setTimeout(() => {
            sta = 1
            rej('超时')
        }, 10000)
        pool.getConnection((err, conn) => {
            if (sta == 1) { return; }
            if (err) { rej(err) } else { res(conn) }
        })
    })
}

module.exports = {
    sqlinit: function (res, cb) {
        forsqlinit().then((conn) => {
            cb && cb(conn)
        }).catch((err) => {
            res.send({
                msg: err || 'fail',
                status: statusobj.fail
            })
        })
    }
}